package com.nzy.ssm.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.nzy.ssm.entity.ItemCategory;
import com.nzy.ssm.mapper.ItemCategoryMapper;

@RestController
public class TestController {

	@Autowired
	private ItemCategoryMapper itemCategoryMapper;
	
	
	@RequestMapping("/index")
	public String index() {
		return "Hello World!！！！";
	}
	
	@RequestMapping("/tree")
	public List<ItemCategory> tree() {
		List<ItemCategory> list = itemCategoryMapper.listAll();
		List<ItemCategory> tree = createTree(list,0);
		return tree;
	}
	
	
	/**
	 * 1、所有的分类列表
	 * 2、父级分类ID
	 * @return
	 */
	public List<ItemCategory> createTree(List<ItemCategory> list,Integer pid){
		List<ItemCategory> tree = new ArrayList<ItemCategory>();
		for (ItemCategory itemCategory : list) {
			if (itemCategory.getPid() == pid) {
				itemCategory.setChidren(createTree(list,itemCategory.getId()));
				tree.add(itemCategory);
			}
		}
		return tree;
	}
	
	
}
